package com.app.couponsharelife.ui.view

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Paint
import androidx.constraintlayout.widget.ConstraintLayout
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.app.couponsharelife.R
import com.app.couponsharelife.data.bean.TaobaoGoodBean
import com.app.couponsharelife.utils.AppUtils
import com.mylhyl.zxing.scanner.encode.QREncode
import java.io.File
import java.text.DecimalFormat

/**
 * Created by guluwa on 2018/11/26.
 */
class GoodSharePicView @JvmOverloads constructor(private val mShareUrl: String,
                                                 private val goodBean: TaobaoGoodBean,
                                                 context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) :
        ConstraintLayout(context, attrs, defStyleAttr) {

    var view: View? = null

    init {
        initView(context)
    }

    private fun initView(context: Context) {
        val df = DecimalFormat("#0.00")
        if (goodBean.title.isNotEmpty()) {
            view = LayoutInflater.from(context).inflate(R.layout.share_pic_layout, null, false)
            view!!.findViewById<TextView>(R.id.tvGoodName).text = "      " + goodBean.title
            view!!.findViewById<TextView>(R.id.tvGoodOriginalPrice).text = String.format("售价：%s元", df.format(goodBean.original_price.toDouble()))
            view!!.findViewById<TextView>(R.id.tvGoodOriginalPrice).paintFlags =
                    view!!.findViewById<TextView>(R.id.tvGoodOriginalPrice).paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
            view!!.findViewById<ImageView>(R.id.imgtype).setImageResource(goodBean.userType)
            view!!.findViewById<TextView>(R.id.tvGoodCouponPrice).text = String.format("%s元", df.format(goodBean.final_price.toDouble()))
            view!!.findViewById<TextView>(R.id.tvprice).text = df.format(goodBean.final_price.toDouble())
            if (goodBean.has_coupon) {
                view!!.findViewById<TextView>(R.id.tvCouponAmount).text = String.format("¥%s", df.format(goodBean.coupon_price.toDouble()))
                view!!.findViewById<TextView>(R.id.tvDiscountsPrice).text = String.format("¥%s", df.format(goodBean.coupon_price.toDouble()))
                view!!.findViewById<TextView>(R.id.tvCouponAmount).visibility = View.VISIBLE
                view!!.findViewById<TextView>(R.id.tvDiscountsPrice).visibility = View.VISIBLE
                view!!.findViewById<TextView>(R.id.tvGoodCouponPriceTitle).text = "券后："
                view!!.findViewById<TextView>(R.id.tvtext1).text = "券后价："
            } else {
                view!!.findViewById<TextView>(R.id.tvDiscountsPrice).visibility = View.GONE
                view!!.findViewById<TextView>(R.id.tvCouponAmount).visibility = View.GONE
                view!!.findViewById<TextView>(R.id.tvGoodCouponPriceTitle).text = "抢："
                view!!.findViewById<TextView>(R.id.tvtext1).text = "抢："
            }
            view!!.findViewById<ImageView>(R.id.ivQrcode).setImageBitmap(QREncode.Builder(context)
                    .setColor(ContextCompat.getColor(context, R.color.black))//二维码颜色
                    .setLogoBitmap(BitmapFactory.decodeResource(view!!.resources, R.mipmap.ic_launcher))
                    .setContents(mShareUrl)//二维码内容
                    .setMargin(0)
                    .setSize(500)
                    .build().encodeAsBitmap())
        }
    }

    fun setImage(file: File) {
        view!!.findViewById<ImageView>(R.id.ivGoodPic).setImageBitmap(BitmapFactory.decodeFile(file.absolutePath))
    }

    fun generateBitmap(): Bitmap {
        view!!.isDrawingCacheEnabled = true
        //图片的宽度为屏幕宽度，高度为wrap_content
        view!!.measure(
                View.MeasureSpec.makeMeasureSpec(AppUtils.getDisplayMetrics(context).widthPixels,
                        View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED))
        //放置mView
        view!!.layout(0, 0, view!!.measuredWidth, view!!.measuredHeight)
        view!!.buildDrawingCache()
        return view!!.drawingCache
    }
}